package com.atguigu.spring.aop.xml;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author wk
 * @create 2021-06-02 20:38
 */
@Component
public class LoggerAspect {

    public void beforeMethod(JoinPoint joinPoint){
        //JoinPoint表示当前通知作用于的连接点，可以通过此对象获取连接点相关信息，例如方法名、参数
        System.out.println("Logger-->前置通知，方法名："+joinPoint.getSignature().getName()+"，参数："+ Arrays.toString(joinPoint.getArgs()));
    }

    public void afterMethod(JoinPoint joinPoint){
        System.out.println("Logger-->后置通知，方法名："+joinPoint.getSignature().getName()+",参数"+Arrays.toString(joinPoint.getArgs()));
    }
    public void afterReturningMethod(JoinPoint joinPoint,Object result){
        System.out.println("Logger-->返回通知，方法名："+joinPoint.getSignature().getName()+",参数"+Arrays.toString(joinPoint.getArgs())+"，返回值"+result);
    }
    public void exceptionMethod(JoinPoint joinPoint,Throwable ex){
        System.out.println("Logger-->异常通知，方法名："+joinPoint.getSignature().getName()+",参数"+Arrays.toString(joinPoint.getArgs())+"，返回值"+ex);
    }
    public Object aroundMethod(ProceedingJoinPoint joinPoint){
        Object result=null;
        try {
            System.out.println("前置通知");
            result = joinPoint.proceed();
            System.out.println("返回通知");
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("异常通知");
        }finally {
            System.out.println("后置通知");
        }
        return result;
    }

}
